package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.model.Announcement;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.AnnouncementViewModel;
import com.swz.dcrm.util.DateUtils;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class AnnouncementDetailFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_content)
    TextView tvContent;

    private Announcement announcement;
    @Inject
    AnnouncementViewModel announcementViewModel;
    public static AnnouncementDetailFragment newInstance() {

        return new AnnouncementDetailFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }
    @Override
    public int layoutId() {
        return R.layout.fragment_announcement_detail;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.announcement_detail_title));
        announcement=new Gson().fromJson(AnnouncementDetailFragmentArgs.fromBundle(getArguments()).getAnnouncement(),Announcement.class);
        tvContent.setText(announcement.getContent());
        tvStartTime.setText(announcement.getCreateTime());
        tvEndTime.setText(DateUtils.dateFormat(announcement.getStartTime(),"yyyy-MM-dd")+
                "至"+DateUtils.dateFormat(announcement.getEndTime(),"yyyy-MM-dd"));
        return true;
    }
    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_del)
    public void del(){
        announcementViewModel.del(announcement.getId())
                .observe(this, announcementBaseResponse -> {
                        if(announcementBaseResponse.isSuccess()){

                            backClick();
                        }else {
                            showToast(announcementBaseResponse.getMsg());
                        }
                });
    }
}
